package me.zhengjie.modules.quartz.task;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.domain.Msg;
import me.zhengjie.business.domain.Zhsq;
import me.zhengjie.business.repository.GsRepository;
import me.zhengjie.business.repository.ZhsqRepository;
import me.zhengjie.inter.service.MpMsgService;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.StoredProcedureQuery;
import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * 结束公示期的照护申请
 * @author xiahongbo
 * @date 2022-07-20
 */
@Slf4j
@Async
@Component
public class GsTask {

    @Autowired
    private GsRepository gsRepository;
    @Autowired
    private ZhsqRepository zhsqRepository;
    @Autowired
    private UserService userService;
    @Autowired
    private MpMsgService mpMsgService;

    @Transactional
    public void gswc() {
        // 当前时间
        String datetime = DateUtil.format(DateUtil.date(), DatePattern.NORM_DATETIME_MS_PATTERN);
        log.info("结束公示期的照护申请 {}" + datetime);
        String ids = gsRepository.queryForUpdateGsZt();
        String[] idsArray = ids.split(",");
        List<Long> list = new ArrayList<>();
        for (int i = 0; i < idsArray.length; i++) {
            Long zhid = Long.parseLong(idsArray[i]);
            list.add(zhid);


            Zhsq zhsq = zhsqRepository.findById(zhid).get();
            Long userId = zhsq.getBisUserId();
            UserDto user = userService.findById(userId);
            Msg msg = new Msg();
            msg.setType("WxTemplate");
            msg.setTimes(0);
            msg.setStatus("0");
            msg.setSendTime(new Timestamp(System.currentTimeMillis()));
            msg.setTarget(user.getWxOpenId());
            JSONObject content = new JSONObject();
            content.put("first","尊敬的用户，您提交的待遇申请有新的状态变动。");
            content.put("keyword1",zhsq.getBisZhsqSnryXm());
            content.put("keyword2",DateTimeUtil.getDateTime(zhsq.getLrsj(), "YYYY年MM月dd日"));
            content.put("keyword3","公示期结束");
            content.put("remark","请通过公众号查看。");

            msg.setContent(content.toJSONString());
            if(!StringUtils.isEmpty(msg.getTarget())){
                mpMsgService.create(msg);
            }
        }
        zhsqRepository.batchUpdateZhzt("07",list);
        gsRepository.updateGsZt();
    }


}
